﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Easily
{
    /// <summary>
    /// 配置条件循环和判断控件的条件
    /// </summary>
    public partial class frmCnd_Loop : Form
    {
        #region 成员变量和属性
        private GeneralIcon MyIcon = null;
        private string condition = "";
        /// <summary>
        /// 循环或判断的条件
        /// </summary>
        public string Condition
        {
            get { return this.condition; }
            set
            {
                this.condition = value;
                if(this.MyIcon.ModuleName == "条件循环")
                    this.MyIcon.code.InsertCode = "while(" 
                        + value + "){\r\n";
                else if ((this.MyIcon.ModuleName == "单分支")
                    || (this.MyIcon.ModuleName == "双分支"))
                    this.MyIcon.code.InsertCode = "if("
                        + value + "){";
                this.MyIcon.code.Comment = "循环条件：" + value;
            }
        }
        #endregion

        #region 方法
        ///==============================================================================
        //frmCnd
        //功能:frmCnd类的构造函数,初始化frmCnd类中的控件的初始状态
        //参数:无
        //返回:无
        ///==============================================================================
        public frmCnd_Loop(GeneralIcon icon)
        {
            InitializeComponent();
            this.MyIcon = icon;
            if (this.MyIcon.code.Save != "")
                this.restore();
        }

        /// <summary>
        /// 从保存信息字符串中恢复控件
        /// </summary>
        private void restore()
        {
            this.Condition = this.MyIcon.code.Save;
            this.textBoxCalculate.Text = this.Condition;
            this.textBoxCalculate.SelectionStart = this.Condition.Length;
        }

        #endregion

        #region 事件

        ///==============================================================================
        //btn_Click
        //功能:根据单击的按钮,设置表达式
        ///==============================================================================
        private void btn_Click(object sender, EventArgs e)
        {
            //[2018]利用Tag记录数据，去除switch判断
            Button btn = sender as Button;
            if(btn.Name == "btnClear")textBoxCalculate.Text = "";
            else
            {
                int index = textBoxCalculate.SelectionStart;
                textBoxCalculate.Text = textBoxCalculate.Text.Insert
                    (index, btn.Tag.ToString());
                textBoxCalculate.SelectionStart = index + btn.Tag.ToString().Length;
            }
        }

        ///==============================================================================
        //comboBoxVariable_DropDown
        //功能:向下拉框中添加变量
        ///==============================================================================
        private void comboBoxVariable_DropDown(object sender, EventArgs e)
        {
            int i;
            //(1) 清空下拉框内容
            this.comboBoxVariable.Items.Clear();
            //(2) 向下拉框中添加程序中的变量的名字
            //添加模块局部变量201512
            //for (i = 0; i < Globaldefine.FrmParent.ActivedrmGraphicFlow.ModuleVariable.Count; i++)
            //{
            //    this.comboBoxVariable.Items.Add(Globaldefine.FrmParent.ActivedrmGraphicFlow.ModuleVariable[i]);
            //}            
            //添加系统变量
            if (Globaldefine.FrmParent.ActivedrmGraphicFlow != null)
            {
                //全局变量
                for (i = 0; i < Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys.Count; i++)
                {
                    this.comboBoxVariable.Items.Add(Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys[i]);
                }
                //传感器局部变量
                foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_Sensor_VariableLocal.Values)
                {
                    this.comboBoxVariable.Items.Add(item);
                }
                //局部变量
                foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableLocal)
                {
                    comboBoxVariable.Items.Add(item);
                }
            }
        }
        ///==============================================================================

        ///==============================================================================
        //comboBoxVariable_SelectedIndexChanged
        //功能:将下拉框中选择的变量添加到表达式中
        ///==============================================================================
        private void comboBoxVariable_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxCalculate.Text += this.comboBoxVariable.SelectedItem.ToString();
        }

        ///==============================================================================
        //textBoxCalculate_KeyUp
        //功能:表达式文本框接收键盘输入
        ///==============================================================================
        private void textBoxCalculate_KeyUp(object sender, KeyEventArgs e)
        {

        }
        /// <summary>
        /// 点击“确定”按钮
        /// </summary>
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Condition = this.textBoxCalculate.Text;
            this.MyIcon.code.Save = this.Condition;//保存控件信息
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 点击“注释说明”按钮
        /// </summary>
        private void btnAddNote_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(this.MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                {
                    this.MyIcon.code.Comment = NE.NoteEdit;
                }
            }
        }
        /// <summary>
        /// 点击“取消注释”按钮
        /// </summary>
        private void btnClearNote_Click(object sender, EventArgs e)
        {
            this.MyIcon.code.Comment = string.Empty;
        }
        #endregion
    }
}